'''
Author: Tyro-yxk tyroyang@foxmail.com
Date: 2024-06-12 20:10:35
LastEditors: Tyro-yxk tyroyang@foxmail.com
LastEditTime: 2024-06-12 22:29:05
FilePath: \python\deldir\table.py
Description:

Copyright (c) 2024 by Tyro-yxk, All Rights Reserved.
'''
import tkinter as tk
import logic
from tkinter import filedialog
import threading

class FastPrintWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.x = 100
        self.y = 100
        self.width = 400
        self.height = 300
        # 设置窗口大小
        self.geometry(f"{self.width}x{self.height}")
        # 设置窗口位置
        self.geometry(f"+{self.x}+{self.y}")
        # self.text = tk.Text(self, height=20, width=40)
        # self.text.pack()
        self.create_widgets()

    def delete_dir(self, tab):
        tab.button_1.config(state="disabled")
        tab.button_2.config(state="disabled")
        self.fast_print(tab, "开始删除文件夹")
        folder_path = filedialog.askdirectory()
        logic.list_files_recursive(self, tab, folder_path)
        print("删除完成")
        self.fast_print(tab, "删除完成")
        tab.button_1.config(state="normal")
        tab.button_2.config(state="normal")

    def add_dir(self, tab):
        tab.button_1.config(state="disabled")
        tab.button_2.config(state="disabled")
        file_path = filedialog.askopenfilename(filetypes=[("日志文件", "*.log")], initialdir="./")
        self.fast_print(tab, "开始恢复文件夹")
        logic.makdir(self, tab, file_path)
        print("恢复完成")
        self.fast_print(tab, "恢复完成")
        tab.button_1.config(state="normal")
        tab.button_2.config(state="normal")


    def create_widgets(self):
        self.create_subwindow_button_a = tk.Button(self, text="删除文件夹",
                                                   command=lambda: self.create_subwindow("删除", self.delete_dir))
        self.create_subwindow_button_a.grid(row=0, column=0, sticky='W', padx=90, pady=40)
        self.create_subwindow_button_b = tk.Button(self, text="恢复文件夹",
                                                   command=lambda: self.create_subwindow("恢复", self.add_dir))
        self.create_subwindow_button_b.grid(row=0, column=1, sticky='E', pady=40)

     

    def fast_print(self, tab, *args, **kwargs):
        print(*args, **kwargs)
        tab.text.insert("end", " ".join(map(str, args)) + "\n")
        tab.text.see("end")
        tab.text.update()


    def on_child_window_close(self, tk):
        self.get_x_y()
        self.geometry(f"+{self.x}+{self.y}")
        # 重新显示主窗口
        tk.destroy()
        self.deiconify()

    def close(self, tk):
        tk.destroy()
        self.destroy()
    def get_x_y(self):
        self.x = self.winfo_x()
        self.y = self.winfo_y()

    def create_subwindow(self, str, func=None):
        self.get_x_y()
        self.withdraw()
        subwindow = tk.Toplevel(self)
        subwindow.geometry(f"{self.width}x{self.height}")
        subwindow.geometry(f"+{self.x}+{self.y}")
        subwindow.title(f"{str}文件夹")

        subwindow.text = tk.Text(subwindow, wrap="word", font=("Courier", 12), width=400, height=10)
        subwindow.text.pack(expand=True, fill="both")

        subwindow.button_1 = tk.Button(subwindow, text=f"{str}", command=lambda: func(subwindow))
        subwindow.button_1.pack(side="left", padx=(90, 0))
        subwindow.button_2 = tk.Button(subwindow, text="返回首页", command=lambda: self.on_child_window_close(subwindow))
        subwindow.button_2.pack(side="right", padx=(0, 90))

        subwindow.protocol("WM_DELETE_WINDOW", lambda: self.close(subwindow))


window = FastPrintWindow()
window.mainloop()
